<template>
  <div class="head-container">
    <div class="nav">
      <img class="menu" src="@/assets/image/burgerBar.svg" data-aos="fade-right" @click="data.showRight = true" />
      <div class="logo-content" data-aos="fade-right" @click="
        goUrl({ key: 0, menuName: $t('home'), path: '/home', open: true })
        ">
        <img class="logo" src="@/assets/image/logo.png" alt="Brand Logo" />
        <div class="brand-name">-</div>
        <div class="brand-name">
          PLAN
        </div>
      </div>
      <div class="f-r-y-c f-right" data-aos="fade-left">
        <Language />
        <van-button class="connect-btn" v-if="!addressStore.address" @click="onConnect">
          {{ $t("connectWallet") }}
        </van-button>
        <van-button class="address-btn" v-else="addressStore.address">
          {{ formatAddress(addressStore.address) }}
        </van-button>
      </div>
    </div>
    <van-popup class="popup" @click-overlay="data.showRight = false" v-model:show="data.showRight" position="left"
      :style="{ height: autoHeight }" :overlay-style="{ background: 'transparent' }">

      <div class="menu-popup">
        <div class="menu-top">
          <img src="@/assets/image/logo.png" alt="Brand Logo" @click="
            goUrl({ key: 0, menuName: $t('home'), path: '/home', open: true })
            " />
          <img src="@/assets/image/close.svg" @click="data.showRight = false" />
        </div>
        <div class="menu-list f-1 f-c-x-c">
          <div v-for="(item, index) in menuList" :key="index">
            <div class="menu" v-if="item.isShow">
              <div class="title">{{ item.menuName }}</div>
              <div class="box">
                <div v-for="(item1, index1) in item.children" :key="index1">
                  <div :class="['meun-item', item1.path == $route.path && 'active']" @click="goUrl(item1)">
                    <span :class="['iconfont', item1.icon]"></span>
                    <span class="meun-name">{{ item1.menuName }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script setup lang="ts">
import { Contract } from "@/utils/wallet/contract";
import Ethereum from "@/utils/wallet/ethereum";
import { userStore } from "@/stores/modules/user";
import { formatAddress } from "@/utils/kit";
import { detectBrowser } from "@/utils/tool";
import { Async_userInfo } from "@/server/api";
import { IUserInfo } from "@/model/user";
const router = useRouter();
const route = useRoute();
const data = reactive({
  showRight: false
});
const autoHeight = ref("100%");
const goUrl = item => {
  if (item.url) return window.open(item.url, "_blank");
  router.push({
    path: item.path
  });
  data.showRight = false;
};
const addressStore = userStore();
let menuList = addressStore.menuList;
const onConnect = async () => {
  await Ethereum.init();
  await Contract.init();
  if (!addressStore.sign) {
    await Contract.signMessage();
    await Async_userInfo<IUserInfo>({ inviteCode: route.query.inviteCode }).then(res => {
      userStore().updateInfo(res);
      setMenu()
      window.location.reload()
    });
  } else {
    setMenu()
  }
};

const setMenu = () => {
  for (let index = 0; index < menuList.length; index++) {
    const temp = menuList[index];
    if (addressStore.sign) {
      temp.isShow = true
    }
  }
}

onMounted(() => {
  onConnect();
  if (detectBrowser() === "Apple Safari") {
    autoHeight.value = `${window.outerHeight}px`;
  }
});
</script>
<style lang="scss">
$theme: #59e457;

.head-container {
  height: 56px;
  position: fixed;
  top: 0px;
  left: auto;
  right: 0px;
  opacity: 1;
  z-index: 1100;
  width: 100%;

  .nav {
    width: 100%;
    height: 56px;
    display: flex;
    box-sizing: border-box;
    align-items: center;
    transition: box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
    box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 4px -1px,
      rgba(0, 0, 0, 0.14) 0px 4px 5px 0px, rgba(0, 0, 0, 0.12) 0px 1px 10px 0px;
    flex-shrink: 0;
    padding: 0 18px 0 12px;
    background-color: #000000bf;
    color: #fff;

    .menu {
      width: 22px;
      height: 14px;
      display: inline-block;
    }

    .logo-content {
      margin-left: 7%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      height: 22px;


      .logo {
        width: auto;
        height: 22px;
      }

      .brand-name {
        color: #000c;
        font-size: 22px;
        word-wrap: break-word;
        display: block;
        font-family: Ethnocentric;
        font-weight: 400;
        text-align: center;
        background: linear-gradient(to right, #06f8a3 0%, #59e457 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
    }


  }

  .nav .menu {
    font-size: 24px;
  }

  .f-right {
    margin-left: auto;
    margin-right: 0;
    display: flex;
    align-items: center;

    .connect-btn {
      background-color: $theme;
      border-color: $theme;
      border-radius: 10px;
      height: 24px;
      color: #000;
      font-weight: bold;
      font-size: 10px;
      padding: 0 18px;
      margin-left: 12px;
    }

    .address-btn {
      margin-left: 2vw;
      width: 100%;
      background: transparent;
      color: $theme;
      border-color: $theme;
      margin-left: 6px;
    }
  }
}

.van-popup--left {
  width: none;
  height: 100%;
  background: transparent;
  right: 0;
  pointer-events: none;
  background-color: rgba(0, 0, 0, 0.5);
}

.popup {
  overflow: scroll;
  position: fixed;
  width: 100%;
  height: 100%;
  pointer-events: painted;
  background: transparent;
  // min-height: 100vh;
}

.menu-popup {
  backdrop-filter: blur(5px);
  color: #fff;
  width: 260px;
  inset: 0px;
  min-height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);

  .menu-top {
    align-items: center;
    display: flex;
    justify-content: space-between;
    padding: 16px;
    padding-bottom: 16px;

    img:nth-child(1) {
      height: 33px;
    }

    img:nth-child(2) {
      height: 24px;
    }
  }

  .menu-list {
    display: flex;
    flex-direction: column;

    .menu {
      width: calc(100% - 32px);
      border-top: 1px solid rgba(255, 255, 255, 0.5);
      margin: 16px 16px 0;
      padding-top: 16px;
    }

    .title {
      color: $theme;
      font-family: Inter, sans-serif;
      font-size: 18px;
      font-weight: 700;
      padding: 4px 16px 0 0;
    }

    .box {
      margin: 8px 0;
    }

    .meun-item {
      width: 100%;
      display: flex;
      color: #fff;
      display: flex;
      font-weight: 500;
      gap: 0.75rem;
      padding-bottom: 0;
      padding-left: 1.5rem;
      padding: 8px 16px;
      font-size: 1rem;

      &:last-child {
        margin-bottom: 0;
      }

      &.active {
        .iconfont {
          color: $theme;
        }

        .meun-name {
          color: $theme;
        }
      }
    }

    .iconfont {
      width: 20px;
      height: 20px;
      color: $theme;
    }
  }
}
</style>
